/*
 * 文件名：Xml2BeanUtil.java
 * 版权： Copyright 2014-2017 北京思源政务通科技有限公司.
 * 描述：〈描述〉
 * 修改人：137127
 * 修改时间：2017年4月17日
 * 修改单号：2017年4月17日
 * 修改内容：〈修改内容〉
 */
package com.zhengtoon.mctoon.conv.utils;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.qitoon.framework.core.exception.BusinessException;
import com.zhengtoon.mctoon.conv.bean.ConvPhotoInfo;
import com.zhengtoon.mctoon.conv.exception.ConvPhotoExceptionCode;
import com.zhengtoon.mctoon.conv.ws.bean.AttachmentResult;
import com.zhengtoon.mctoon.conv.ws.bean.LetterResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * xml转为javabean
 * 
 * @author 137127
 * @version [1.0.0, 2017年4月17日]
 */
public class Xml2BeanUtil {
    private static Logger logger = LoggerFactory.getLogger(Xml2BeanUtil.class);

    /**
     * Xml转为java对象
     * @author 137127
     * @param xmlStr xml内容
     * @param t 类型
     * @return 
     * @exception BusinessException GovHotlineExceptionCode.PARSE_XML_FAIL
     */
    public static <T> T convert2Bean(String xmlStr, Class<T> t) {
        try {

            ObjectMapper xmlMapper = new XmlMapper();
            xmlMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
            xmlMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);

            T value = xmlMapper.readValue(xmlStr, t);
            return value;
        } catch (Exception e) {
            logger.error("Xml2BeanUtil转失败",e.getMessage());
            throw new BusinessException(ConvPhotoExceptionCode.PARSE_XML_FAIL);
        }
    }

    public static void main(String[] args) {
        LetterResult result = convert2Bean("<result>\n" +
                "  <result_type>USER_DEFINE</result_type>\n" +
                "  <result_status>SUCCESS</result_status>\n" +
                "  <user_define_information>\n" +
                "    <letter_lists>\n" +
                "      <not_accept_letter_list>\n" +
                "        <rows count=\"1\">\n" +
                "          <row id=\"0\">\n" +
                "            <sys_letter_id><![CDATA[BJT_1492246349446]]></sys_letter_id>\n" +
                "            <letter_id><![CDATA[AH17041800005]]></letter_id>\n" +
                "            <letter_title><![CDATA[北京通测试信件-标题-1492246349446]]></letter_title>\n" +
                "            <writer><![CDATA[BJT1#北京通用户]]></writer>\n" +
                "            <submit_time><![CDATA[2017-04-18 11:26:46.0]]></submit_time>\n" +
                "          </row>\n" +
                "        </rows>\n" +
                "      </not_accept_letter_list>\n" +
                "      <finish_letter_list>\n" +
                "        <rows count=\"0\" />\n" +
                "      </finish_letter_list>\n" +
                "    </letter_lists>\n" +
                "  </user_define_information>\n" +
                "</result>", LetterResult.class) ;

        System.out.println(result.getLetterInformation().getLetterLists().getNotAcceptLetterList());
    }

}
